package yangsw87.swing;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.ImageIcon;
import javax.swing.SwingConstants;
import javax.swing.JCheckBox;

import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;
import javax.swing.JList;
import java.awt.Dimension;
import javax.swing.AbstractListModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

public class BComp extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtBtn;
	private JPasswordField pwdBtn;
	private JList list1; // 필드 생성
	private JList list2; // 필드 생성

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			BComp dialog = new BComp();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public BComp() {
		setTitle(":: 기본 컴퍼넌트 ::");
		setBounds(100, 100, 483, 529);
		setLocationRelativeTo(null);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setLayout(new FlowLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		{
			JButton imgBtn = new JButton("이미지버튼");
			imgBtn.setToolTipText("이미지버튼입니다");
			
			imgBtn.setIcon(new ImageIcon(BComp.class.getResource("/yangsw87/swing/images/icon/icon.png")));
			//ImageIcon rollover = new ImageIcon("/yangsw87/swing/images/icon/icon2.png");
			
			imgBtn.setRolloverIcon(new ImageIcon(BComp.class.getResource("/yangsw87/swing/images/icon/icon2.png")));
			
			imgBtn.setFont(new Font("맑은 고딕", Font.BOLD, 14));
			contentPanel.add(imgBtn);
		}
		{
			JLabel imgLabel = new JLabel("이미지레이블");
			imgLabel.setIcon(new ImageIcon(BComp.class.getResource("/yangsw87/swing/images/icon/icon2.png")));
			imgLabel.setFont(new Font("맑은 고딕", Font.BOLD, 14));
			contentPanel.add(imgLabel);
		}
		// 체크 버튼 선언
			JCheckBox chkjungeun = new JCheckBox("정은");
			chkjungeun.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
//					int selected = 1;
//					if(e.getStateChange()==ItemEvent.SELECTED)
//						selected = 1; // 선택됨
//					else
//						selected = -1; // 비선택
//					if(selected == 1)
//						JOptionPane.showMessageDialog(null, e.getItem()+" 을 선택! ");
					
					JCheckBox c = (JCheckBox)e.getSource();
					if(c.isSelected())
						txtBtn.setText(c.getText()); // 글자를 받아 올 수 있음!
					else
						txtBtn.setText("");
					
				}
			});
			contentPanel.add(chkjungeun);
		
			JCheckBox chkboyoung = new JCheckBox("보영");
			chkboyoung.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					JCheckBox c = (JCheckBox)e.getSource();
					if(c.isSelected())
						txtBtn.setText(c.getText());
					else
						txtBtn.setText("");
				}
			});
			contentPanel.add(chkboyoung);
		
			JCheckBox chkiu = new JCheckBox("아이유");
			chkiu.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					JCheckBox c = (JCheckBox)e.getSource();
					if(c.isSelected())
						txtBtn.setText(c.getText());
					else
						txtBtn.setText("");
				}
			});
			contentPanel.add(chkiu);
		// 체크 버튼 선언 끝
			
		// 라디오 버튼 선언
			JRadioButton radjungeun = new JRadioButton("정은");
			radjungeun.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					JRadioButton r = (JRadioButton)e.getSource(); // 형변환을 해줘야함. (JRadioButton)e.
					if(r.isSelected())
						txtBtn.setText(r.getText());
					else
						txtBtn.setText("");
				}
			});
			contentPanel.add(radjungeun);
		
			JRadioButton radboyoung = new JRadioButton("보영");
			radboyoung.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					JRadioButton r = (JRadioButton)e.getSource(); // 형변환을 해줘야함. (JRadioButton)e.
//					if(radjungeun.)
					if(r.isSelected())
							txtBtn.setText(r.getText());
						else
							txtBtn.setText("");
				}
			});
			contentPanel.add(radboyoung);
		
			JRadioButton radiu = new JRadioButton("아이유");
			radiu.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					JRadioButton r = (JRadioButton)e.getSource(); // 형변환을 해줘야함. (JRadioButton)e.
					if(r.isSelected())
						txtBtn.setText(r.getText());
					else
						txtBtn.setText("");
				}
			});
			contentPanel.add(radiu);
		// 라디오 버튼 선언 끝		
		// 라디오 버튼 그룹 생성 및 추가
		ButtonGroup rgroup = new ButtonGroup();
		rgroup.add(radjungeun);
		rgroup.add(radboyoung);
		rgroup.add(radiu);
		
		txtBtn = new JTextField();
		contentPanel.add(txtBtn);
		txtBtn.setColumns(10);
		
		JButton txt1Btn = new JButton("입력문자보기");
		txt1Btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null, txtBtn.getText());
				// textField에 입력된 문자 출력
			}
		});
		contentPanel.add(txt1Btn);
		
		pwdBtn = new JPasswordField();
		pwdBtn.setEchoChar('♥');
		pwdBtn.setColumns(16);
		contentPanel.add(pwdBtn);
		
		JButton pwd1Btn = new JButton("입력문자보기");
		pwd1Btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
//				String pwd = String.valueOf(pwdBtn.getPassword());
//				JOptionPane.showMessageDialog(null, pwd);
				JOptionPane.showMessageDialog(null, String.valueOf(pwdBtn.getPassword()));
				// pwdBtn.getPassword()와 비교
				// fwdField에 입력된 문자 출력
			}
		});
		contentPanel.add(pwd1Btn);
		
		JScrollPane scrollPane1 = new JScrollPane();
		scrollPane1.setPreferredSize(new Dimension(150, 100));
		contentPanel.add(scrollPane1);
		
		// JList에 추가할 항목을 배열로 생성
		String[] data = {"정은", "보영", "아이유", "수지", "혜교", "지현", "유리"};
		// 생성된 배열을 JList의 생성자에 주입
		list1 = new JList(data);	
	
		scrollPane1.setViewportView(list1);
		
		JScrollPane scrollPane2 = new JScrollPane();
		scrollPane2.setPreferredSize(new Dimension(150, 100));
		contentPanel.add(scrollPane2);
		
		list2 = new JList();
		list2.addMouseListener(new MouseAdapter() {
			// JList에 마우스 이벤트 추가
			@Override
			public void mouseClicked(MouseEvent e) {
				if(e.getClickCount() == 2) {
					JOptionPane.showMessageDialog(null, list2.getSelectedIndex() + "/"
							+ list2.getSelectedValue());
				}
			}
		});
		list2.setModel(new AbstractListModel() {
			String[] values = new String[] {"정은", "보영", "아이유", "수지", "혜교", "지현", "유리"};
			public int getSize() {
				return values.length;
			}
			public Object getElementAt(int index) {
				return values[index];
			}
		});
		scrollPane2.setViewportView(list2);
		
		JButton listBtn = new JButton("리스트선택보기");
		listBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null, list1.getSelectedIndex() + "/"
						+ list1.getSelectedValue());
			}
		});
		contentPanel.add(listBtn);
		
		JScrollPane scrollPane3 = new JScrollPane();
		scrollPane3.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane3.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane3.setPreferredSize(new Dimension(150, 100));
		contentPanel.add(scrollPane3);
		
		JTextArea txtrSdfds = new JTextArea();
		txtrSdfds.setText("sdfdssdfdsfsdfsdfdsfsaffsfsafsa");
		scrollPane3.setViewportView(txtrSdfds);
		
//		String[] data = {"정은", "보영", "아이유", "수지", "혜교", "지현", "유리"};
		final JComboBox comboBox1 = new JComboBox(data);
		comboBox1.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				JOptionPane.showMessageDialog(null, comboBox1.getSelectedIndex() + "/"
						+ comboBox1.getSelectedItem()); // 이거 바꾸기 2번이 뜸!
			}
		});
		contentPanel.add(comboBox1);
		
		JComboBox comboBox2 = new JComboBox();
		comboBox2.setModel(new DefaultComboBoxModel(new String[] {"정은", "보영", "아이유", "수지", "혜교", "지현", "유리"}));
		contentPanel.add(comboBox2);
			
			
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okBtn = new JButton("OK");
				okBtn.setActionCommand("OK");
				buttonPane.add(okBtn);
				getRootPane().setDefaultButton(okBtn);
			}
		}
	}

}
